//
//  CZNavigationController.swift
//  CZVVeiBo
//
//  Created by smile on 2017/7/29.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit

class CZNavigationController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 重新设置pop手势代理
        interactivePopGestureRecognizer?.delegate = self
    }
    
    // 手势代理方法
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        return childViewControllers.count != 1
    }
    // 重写push方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        // 如果是二级界面及其以下界面才设置统一的返回按钮
        if childViewControllers.count > 0 {
            // 定义title
            var title = "返回"
            // 如果是二级界面
            if childViewControllers.count == 1 {
                title = childViewControllers.first?.title ?? "返回"
            }
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imgName: "navigationbar_back_withtext", target: self, action: #selector(backClick))
            
        }
        super.pushViewController(viewController, animated: animated)
    }
    // 返回事件
    @objc private func backClick(){
        popViewController(animated: true)
    }

}
